<template>
  <div class="">
    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="memberInfoRef" :model="form" :rules="rules" label-width="80px">
		  
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
       
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
		
		<el-form-item label="生日" prop="birthday" >
		  <el-date-picker clearable
			style="width: 100%;"
		    v-model="form.birthday"
		    type="date"
			:default-value="new Date(1990,0,1)"
		    value-format="YYYY-MM-DD"
		    placeholder="请选择生日">
		  </el-date-picker>
		</el-form-item>
		
		<el-form-item label="性别" prop="gender" >
		  <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%;">
		    <el-option
		      v-for="dict in bus_gender"
		      :key="dict.value"
		      :label="dict.label"
		      :value="dict.value"
		    ></el-option>
		  </el-select>
		</el-form-item>
        
        <el-form-item label="归属员工" prop="belUserId">
			<el-select v-model="form.belUserId" placeholder="请选择归属员工" style="width: 100%;">
				<el-option v-for="user in listUsers" :key="user.userId" :label="user.nickName" :value="user.userName" />
			</el-select>
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MemberInfo">
import { addMemberInfo } from "@/api/crm/memberInfo";

const { proxy } = getCurrentInstance();
const { bus_gender, bus_rem_level, bus_reg_way } = proxy.useDict('bus_gender', 'bus_rem_level', 'bus_reg_way');
import { listUser } from "@/api/system/user";

const open = ref(false);
const title = ref("创建会员信息");

const listUsers = ref([]);

const data = reactive({
  form: {gender: '0'},
  rules: {
	    name: [
			{ required: true, message: '请输入会员名称', trigger: 'blur' },
		],
		phone:[
			{ required: true, message: '请输入联系电话', trigger: 'blur' },
			{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }
		],
		birthday:[
			{ required: true, message: '请选择生日', trigger: ['blur','change'] },
		],
		gender:[
			{ required: true, message: '请选择性别', trigger: ['blur','change'] },
		],
		belUserId:[
			{ required: true, message: '请选择归属员工', trigger: ['blur','change']},
		]
  }
});

const {  form, rules } = toRefs(data);

const emit = defineEmits(['saveOk'])

function openWin(){
	open.value = true;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    gender: '0',
    phone: null,
    meId: null,
    grade: null,
    birthday: null,
    belUserId: null,
    belDeptId: null
  
  };
  proxy.resetForm("memberInfoRef");
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["memberInfoRef"].validate(valid => {
    if (valid) {
		addMemberInfo(form.value).then(response => {
		  proxy.$modal.msgSuccess("新增成功");
		  
		  cancel();
		  emit('saveOk',response.data)
		});
    }
  });
}


function loadListUsers(){
	listUser().then(rsp=>{
		listUsers.value = rsp.rows;
	})
}

loadListUsers();

defineExpose({
	openWin
})
</script>
